// 安装  mongoose

const mongoose = require("mongoose");

// 连接数据库       协议       账号 密码    地址          数据库
mongoose.connect("mongodb://root:pwd@127.0.0.1:27018/admin");

mongoose.connection.once("open", async () => {
  console.log("连接成功");

  const MyModel = mongoose.model("book", new mongoose.Schema({ name: String }));

  MyModel.deleteOne({ name: 'John Doe' })
    .then((res) => {
      console.log("删除成功", res);
    })
    .catch((err) => {
      console.log("删除失败", err);
    });

  // const MyModel = mongoose.model("book", new mongoose.Schema({ name: String }));
  // // Works
  // const john = new MyModel({ name: "John Doe", age: 30 });
  // const data = await john.save();
  // console.log('data',data);
});

mongoose.connection.once("error", () => {
  console.log("连接失败");
});

mongoose.connection.once("close", () => {
  console.log("连接关闭");
});

// setTimeout(() => {
//   mongoose.disconnect();
// }, 3000);
